#include <iostream>
/*
 * @lc app=leetcode.cn id=29 lang=cpp
 *
 * [29] 两数相除
 */

// @lc code=start
class Solution {
public:
    int divide(int dividend, int divisor) {
        if (dividend == 0) return 0;
        if (dividend == -2147483648 && divisor == -1) return 2147483647;
        else if (divisor == 1) return dividend;
        else if (divisor == -1) return -dividend;
        else if (divisor == -2147483648) {
            if (dividend == divisor) return 1;
            else return 0;
        } 
        int signal = (dividend ^ divisor) >= 0 ? 1 : -1;
        return signal * div(abs(dividend), abs(divisor));
    }

    // 10 3
    int div(int a, int b) {
        if (a < b) return 0;
        int step = 1, eb = b;
        while (a - b >= b) {
            step += step;
            b += b;
        }
        return step + div(a - b, eb);
    }
};
// @lc code=end

